<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Repositories\AdminMenuRepository;
use App\Models\AdminMenu;
use App\Http\Requests\Admin\CreateMenuRequest;
use Illuminate\Validation\Rule;

class MenuController extends Controller
{
    protected $Repo;

    public function __construct(AdminMenuRepository $repo)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $menus = $this->Repo->getMenuLists();
        // dd($menus);
        return view('admin.menus.index', compact('menus'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $topMenus = $this->Repo->getTopMenus();
        return view('admin.menus.create', compact('topMenus'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateMenuRequest $request)
    {
        $res = $this->Repo->addMenu($request);
        if (is_object($res)) {
            $data = [
                'status' => 0,
                'msg' => '添加菜单成功',
                'url' => route('menus.index')
            ];
        } else {
            $data = [
                'status' => 2,
                'msg' => '添加菜单失败'
            ];
        }
        return json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $menu = AdminMenu::find($id);
        $topMenus = $this->Repo->getTopMenus($id);

        return view('admin.menus.edit', compact('menu', 'topMenus'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'title' => ['required', Rule::unique('admin_menu')->ignore($id)],
            'route' => 'required'
        ]);
        $menu = $this->Repo->updateMenu($request, $id);

        $data = [
            'status' => 0,
            'msg'    => '菜单更新成功',
            'url'    => route('menus.index')
        ];

        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->Repo->deleteMenu($id);
        $data = [
            'status' => 0,
            'msg'    => '菜单删除成功',
            'url'    => route('menus.index')
        ];
        return json_encode($data);
    }

    //批量删除
    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $this->Repo->deleteMenuLists($ids);
            $data = [
                'status' => 0,
                'msg'    => '菜单删除成功',
                'url'    => route('menus.index')
            ];
            return json_encode($data);
        }
    }
}
